﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onAppendStudent(QString classId, QString id, QString name, QString className, QString score, QString reScore, int seme)
{
    m_dataModel->insertRow(m_dataModel->rowCount());
    m_dataModel->setData(m_dataModel->index(m_dataModel->rowCount()-1,0),classId,Qt::DisplayRole);
    m_dataModel->setData(m_dataModel->index(m_dataModel->rowCount()-1,1),id,Qt::DisplayRole);
    m_dataModel->setData(m_dataModel->index(m_dataModel->rowCount()-1,2),name,Qt::DisplayRole);
    m_dataModel->setData(m_dataModel->index(m_dataModel->rowCount()-1,3),className,Qt::DisplayRole);
    m_dataModel->setData(m_dataModel->index(m_dataModel->rowCount()-1,4),score,Qt::DisplayRole);
    m_dataModel->setData(m_dataModel->index(m_dataModel->rowCount()-1,5),reScore,Qt::DisplayRole);
    m_dataModel->setData(m_dataModel->index(m_dataModel->rowCount()-1,6),seme,Qt::DisplayRole);
    updataClassNames();
    updataClassIds();
    updataStudentNames();

}

void MainWindow::onOutPut1(QString className, QString filePath)
{
    QFile file(filePath);
    if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
        QTextStream stream(&file);
        stream.setCodec("UTF-8");
        stream << className << QString("不及格名单.\n").toUtf8();
        for(int i = 0;i < m_classIds.count(); ++i){
            stream << QString("班级号:").toUtf8() << m_classIds.at(i) << "\n";
            stream <<QString("学号\t姓名\t成绩\n").toUtf8();
            for(int j = 0;j < m_dataModel->rowCount(); ++j){
                if(m_dataModel->data(m_dataModel->index(j,0)).toString() == m_classIds.at(i) && m_dataModel->data(m_dataModel->index(j,3)).toString() == className && m_dataModel->data(m_dataModel->index(j,4)).toDouble() < 60.0){
                    stream << m_dataModel->data(m_dataModel->index(j,1)).toString() << "\t";
                    stream << m_dataModel->data(m_dataModel->index(j,2)).toString() << "\t";
                    stream << m_dataModel->data(m_dataModel->index(j,4)).toString() << "\n";
                }
            }
            stream <<"\n\n";
        }
        file.close();
        QMessageBox::information(0,"提示","保存成功!");
    }
}

void MainWindow::onOutPut2(QString className, QString savePath)
{
    QFile file(savePath);
    if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
        QTextStream stream(&file);
        stream.setCodec("UTF-8");
        stream << className << QString("年级前五名.\n").toUtf8();
            stream <<QString("学号\t姓名\t成绩\n").toUtf8();
            m_dataModel->sort(4,Qt::DescendingOrder);
            int count = 0;
            for(int i = 0;i < m_dataModel->rowCount() && count < 5;++i){
                if(m_dataModel->data(m_dataModel->index(i,3)).toString() == className){
                count++;
                stream << m_dataModel->data(m_dataModel->index(i,1)).toString() << "\t";
                stream << m_dataModel->data(m_dataModel->index(i,2)).toString() << "\t";
                stream << m_dataModel->data(m_dataModel->index(i,4)).toString() << "\n";

                }
            }
            stream <<"\n\n";

        file.close();
        QMessageBox::information(0,"提示","保存成功!");
    }
}

void MainWindow::onOutPut3(QString className, QString filePath)
{
    QFile file(filePath);
    if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
        QTextStream stream(&file);
        stream.setCodec("UTF-8");
        stream << className << QString("选修者.\n").toUtf8();
            stream <<QString("学号\t姓名\n").toUtf8();

        for(int i = 0;i < m_dataModel->rowCount();++i){
                if(m_dataModel->data(m_dataModel->index(i,3)).toString() == className){

                stream << m_dataModel->data(m_dataModel->index(i,1)).toString() << "\t";
                stream << m_dataModel->data(m_dataModel->index(i,2)).toString() << "\n";
                }
        }
        stream <<"\n\n";

        file.close();
        QMessageBox::information(0,"提示","保存成功!");
    }
}

void MainWindow::onOutPut4(QString studentName, int seme, QString filePath)
{
    QFile file(filePath);
    if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
        QTextStream stream(&file);
        stream.setCodec("UTF-8");
        stream << studentName << QString("第").toUtf8() << seme << QString("学期成绩单\n").toUtf8();
            stream <<QString("课程名\t成绩\t补考成绩\n").toUtf8();

            for(int i = 0;i < m_dataModel->rowCount();++i){
                if(m_dataModel->data(m_dataModel->index(i,2)).toString() == studentName && m_dataModel->data(m_dataModel->index(i,6)).toInt() == seme){

                stream << m_dataModel->data(m_dataModel->index(i,3)).toString() << "\t";
                stream << m_dataModel->data(m_dataModel->index(i,4)).toString() << "\t";
                stream << m_dataModel->data(m_dataModel->index(i,5)).toString() << "\n";
            }
        }
        stream <<"\n\n";

        file.close();
        QMessageBox::information(0,"提示","保存成功!");
    }
}

void MainWindow::onOutPut5(QString className, QString filePath)
{
    QFile file(filePath);
    if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
        QTextStream stream(&file);
        stream.setCodec("UTF-8");
        stream << className << QString("班级平均分排名.\n").toUtf8();
        stream <<QString("班级号\t平均分\t排名\n").toUtf8();

        QMap<double,QString> map;
        for(int i = 0;i < m_classIds.count(); ++i){

            double total = 0.0;
            int count = 0;
            for(int j = 0;j < m_dataModel->rowCount();++j){
                if(m_dataModel->data(m_dataModel->index(j,0)).toString() == m_classIds.at(i) && m_dataModel->data(m_dataModel->index(j,3)).toString() == className){
                    count++;
                    total += m_dataModel->data(m_dataModel->index(j,4)).toDouble();
                }

            }
            if(count > 0){

                map.insert(total/count,m_classIds.at(i));
            }
        }
        QStringList classIds = map.values();
        QList<double> averages = map.keys();

        for(int i = map.count() - 1;i >= 0; --i){
            stream << classIds.at(i) << QString("\t").toUtf8() << averages.at(i) <<  QString("\t").toUtf8() << QString::number(map.count() - i).toUtf8() <<"\n";
        }


        stream <<"\n\n";

        file.close();
        QMessageBox::information(0,"提示","保存成功!");
    }
}

void MainWindow::onOutPut6(QString className, QString classid, QString filePath)
{
    QFile file(filePath);
    if(file.open(QIODevice::WriteOnly | QIODevice::Text)){
        QTextStream stream(&file);
        stream.setCodec("UTF-8");
        stream << classid <<QString("班级").toUtf8()<< className << QString("成绩单.\n").toUtf8();
        stream <<QString("学号\t姓名\t成绩\n").toUtf8();

        QStringList ids;
        for(int i = 0;i < m_dataModel->rowCount();++i){
            if(m_dataModel->data(m_dataModel->index(i,0)).toString() == classid && m_dataModel->data(m_dataModel->index(i,3)).toString() == className){
                ids.append(m_dataModel->data(m_dataModel->index(i,1)).toString());
            }
        }
        ids.sort();


        for(int i = 0;i < ids.count();++i){
            for(int j = 0; j < m_dataModel->rowCount();++j){
                if(ids.at(i) == m_dataModel->data(m_dataModel->index(j,1)).toString() && m_dataModel->data(m_dataModel->index(j,3)).toString() == className){
                    stream << ids.at(i).toUtf8()<<"\t"<<m_dataModel->data(m_dataModel->index(j,2)).toString().toUtf8()<<"\t"<<m_dataModel->data(m_dataModel->index(j,4)).toString().toUtf8()<<"\n";
                }
            }
        }

        stream <<"\n\n";

        file.close();
        QMessageBox::information(0,"提示","保存成功!");
    }
}


void MainWindow::on_actOpen_triggered()
{


    QFile file(QFileDialog::getOpenFileName(0,"选择文件",QDir::homePath(),"文本文件(*.txt);;所有文件(*.*)"));
    if(file.exists()){
        m_dataModel->clear();
        if(file.open(QIODevice::ReadOnly)){
            QTextStream stream(&file);
            stream.setCodec("UTF-8");
            QStringList heads = stream.readLine().trimmed().split("\t",Qt::SkipEmptyParts);
            m_dataModel->setColumnCount(heads.count());
            for(int i = 0; i < heads.count(); ++i){
                m_dataModel->setHeaderData(i,Qt::Horizontal,heads.at(i));
            }
            for(;!stream.atEnd();){

                QStringList datas = stream.readLine().trimmed().split("\t");

                m_dataModel->insertRow(m_dataModel->rowCount());
                for(int i = 0;i < datas.count();++i){
                    m_dataModel->setData(m_dataModel->index(m_dataModel->rowCount()-1,i),datas.at(i),Qt::DisplayRole);
                }

                bool flag = false;
                int index = -1;
                for(int i = 0; i < m_students.count();++i){
                    if(m_students.at(i)->getID() == datas.at(1)){
                        flag = true;
                        index = i;
                        break;
                    }
                }
                if(flag == false){
                    student* s = new student(datas.at(0),datas.at(1),datas.at(2));
                    s->appendClassName(datas.at(3));
                    s->appendClassScore(datas.at(4).toDouble());
                    s->appendClassReScore(datas.at(5).toDouble());
                    s->appendClassSemester(datas.at(6).toInt());
                    m_students.append(s);
                }else{
                    student* s = m_students.at(index);
                    s->appendClassName(datas.at(3));
                    s->appendClassScore(datas.at(4).toDouble());
                    s->appendClassReScore(datas.at(5).toDouble());
                    s->appendClassSemester(datas.at(6).toInt());
                }

            }
            file.close();
        }
    }
    updataClassNames();
    updataClassIds();
    updataStudentNames();
}

void MainWindow::init()
{
    m_dataModel = new QStandardItemModel(this);

    ui->tvStudentInfo->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvStudentInfo->setModel(m_dataModel);
    ui->tvStudentInfo->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

void MainWindow::updataClassNames()
{
    m_classNames.clear();
    for(int i = 0;i < m_dataModel->rowCount();++i){
        if(!m_classNames.contains(m_dataModel->data(m_dataModel->index(i,3)).toString())){
            m_classNames.append(m_dataModel->data(m_dataModel->index(i,3)).toString());
        }
    }

}

void MainWindow::updataClassIds()
{
    m_classIds.clear();
    for(int i = 0;i < m_dataModel->rowCount();++i){
        if(!m_classIds.contains(m_dataModel->data(m_dataModel->index(i,0)).toString())){
            m_classIds.append(m_dataModel->data(m_dataModel->index(i,0)).toString());
        }
    }
}

void MainWindow::updataStudentNames()
{
    m_studentNames.clear();
    for(int i = 0;i < m_dataModel->rowCount();++i){
        if(!m_studentNames.contains(m_dataModel->data(m_dataModel->index(i,2)).toString())){
            m_studentNames.append(m_dataModel->data(m_dataModel->index(i,2)).toString());
        }
    }
}


void MainWindow::on_combSemester_currentIndexChanged(int index)
{
    if(index == 1){
        for(int i = 0;i < m_dataModel->rowCount(); ++i){
            ui->tvStudentInfo->hideRow(i);
            if(m_dataModel->data(m_dataModel->index(i,6)).toInt() == 1){
                ui->tvStudentInfo->showRow(i);
            }
        }
    }else if(index == 2){
        for(int i = 0;i < m_dataModel->rowCount(); ++i){
            ui->tvStudentInfo->hideRow(i);
            if(m_dataModel->data(m_dataModel->index(i,6)).toInt() == 2){
                ui->tvStudentInfo->showRow(i);
            }
        }
    }else if(index == 0){
        for(int i = 0;i < m_dataModel->rowCount(); ++i){
            ui->tvStudentInfo->showRow(i);

        }
    }
}


void MainWindow::on_checkBox_stateChanged(int arg1)
{
    if(!ui->checkBox->isChecked()){
        ui->tvStudentInfo->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }else{
        ui->tvStudentInfo->setEditTriggers(QAbstractItemView::EditTrigger::AllEditTriggers);
    }
}


void MainWindow::on_btnAppend_clicked()
{
    Dialog d;
    connect(&d,SIGNAL(appendStudent(QString,QString,QString,QString,QString,QString,int)),this,SLOT(onAppendStudent(QString,QString,QString,QString,QString,QString,int)));
    d.exec();
}


void MainWindow::on_btnDelete_clicked()
{
    if(ui->tvStudentInfo->currentIndex().isValid()){
        m_dataModel->removeRow(ui->tvStudentInfo->currentIndex().row());
    }
    updataClassNames();
    updataClassIds();
    updataStudentNames();
}


void MainWindow::on_actSave_triggered()
{
    QFile file(QFileDialog::getSaveFileName(0,"选择保存位置",QDir::homePath(),"文本文件(*.txt);;所有文件(*.*)"));
    if(file.open(QIODevice::WriteOnly )){
        QTextStream stream(&file);
        stream.setCodec("UTF-8");
        QString head;
        for(int i = 0;i < m_dataModel->columnCount();++i){
            head += m_dataModel->headerData(i,Qt::Horizontal).toString()+"\t";
        }
        head = head.trimmed();
        head += "\n";
        stream << head;
//        file.write(head.toUtf8());

        for(int i = 0;i < m_dataModel->rowCount();++i){
            QString line;
            for(int j = 0;j < m_dataModel->columnCount();++j){
                line += m_dataModel->data(m_dataModel->index(i,j)).toString() + "\t";
            }
            line = line.trimmed();
            line += "\n";
            stream << line;
//            file.write(line.toUtf8());
        }

    }
    file.close();
}


void MainWindow::on_btnOutput1_clicked()
{
    output1* o = new output1(m_classNames);
    connect(o,SIGNAL(outPut1(QString,QString)),this,SLOT(onOutPut1(QString,QString)));
        o->exec();
    o->deleteLater();
}


void MainWindow::on_btnOutput2_clicked()
{
    output2* o = new output2(m_classNames);
    connect(o,SIGNAL(outPut2(QString,QString)),this,SLOT(onOutPut2(QString,QString)));
    o->exec();
    o->deleteLater();

}


void MainWindow::on_btnOutput3_clicked()
{
    output3* o = new output3(m_classNames);
    connect(o,SIGNAL(outPut3(QString,QString)),this,SLOT(onOutPut3(QString,QString)));
    o->exec();
    o->deleteLater();
}


void MainWindow::on_btnOutput4_clicked()
{
    output4* o = new output4(m_studentNames);
    connect(o,SIGNAL(outPut4(QString,int,QString)),this,SLOT(onOutPut4(QString,int,QString)));
    o->exec();
    o->deleteLater();
}


void MainWindow::on_btnOutput5_clicked()
{
    output5* o = new output5(m_classNames);
    connect(o,SIGNAL(outPut5(QString,QString)),this,SLOT(onOutPut5(QString,QString)));
    o->exec();
    o->deleteLater();
}


void MainWindow::on_btnOutput6_clicked()
{
    output6* o = new output6(m_classNames,m_classIds);
    connect(o,SIGNAL(outPut6(QString,QString,QString)),this,SLOT(onOutPut6(QString,QString,QString)));
    o->exec();
    o->deleteLater();
}

